#########################
#   Author : Lion yu    #
#   Date : 2023/05/06   #
#   Id : SM2772         #
#########################
from handle import Handle, Log

# 查询项目 
def ProjectQuery(pageToken="\"\""):
    req_info = Handle.req_info(
        api_index='ProjectQuery', 
        body_change={"pageToken":pageToken}
        )
    response = Handle.api_req(req_info)
    if response['code'] == 200:
        return response
    else:
        Log.error(f"查询项目 返回异常 ==> {response}\n")
        return

# 查询项目任务
def ProjectTaskQuery(projectId:str, pageToken="\"\"", q="\"\""):
    req_info = Handle.req_info(
        api_index='ProjectTaskQuery', 
        path_change={"projectId" : projectId}, 
        body_change={"pageToken" : pageToken, "q" : q}
        )
    response = Handle.api_req(req_info)
    try:
        if response['code'] == 200:
            return response
        else:
            Log.error(f"查询项目任务 返回异常 ==> {response}\n")
            return
    except Exception as e:
        Log.error(e)
        return

# 列出任务动态
def TaskActivityList(taskId, pageToken="\"\""):
    req_info = Handle.req_info(
        api_index='TaskActivityList', 
        path_change={"taskId" : taskId}, 
        body_change={"pageToken" : pageToken}
        )
    response = Handle.api_req(req_info)
    try:
        if response['code'] == 200:
            return response
        else:
            Log.error(f"查询项目任务 返回异常 ==> {response}\n")
            return
    except Exception as e:
        Log.error(e)
        return
    
# 获取项目任务类型
def ScenarioFieldConfigSearch(projectId:str, pageToken="\"\""):
    req_info = Handle.req_info(
        api_index='ScenarioFieldConfigSearch',
        path_change={"projectId" : projectId},
        body_change={"pageToken" : pageToken}
    )
    response = Handle.api_req(req_info)
    try:
        if response['code'] == 200:
            return response
        else:
            Log.error(f"获取项目任务类型 返回异常 ==> {response}\n")
            return
    except Exception as e:
        Log.error(e)
        return
        
# 获取项目缺陷等级、自定义字段（项目阶段）
def CustomfieldSearch(projectId:str, sfcId:str, pageToken="\"\"", q="\"\""):
    if len(sfcId) == 0:
        Log.error("sfcId 字段不可为空")
        return
    req_info = Handle.req_info(
        api_index='CustomfieldSearch',
        path_change={"projectId" : projectId},
        body_change={"pageToken" : pageToken, "sfcId" : f'"{sfcId}"', "q" : f'"{q}"'}
    )
    response = Handle.api_req(req_info)
    try:
        if response['code'] == 200:
            return response
        else:
            Log.error(f"获取项目缺陷等级、自定义字段 返回异常 ==> {response}\n")
            return
    except Exception as e:
        Log.error(e)
        return
    
# 搜索项目工作流状态
def TaskflowstatusSearch(projectId:str, tfsIds:str):
    if len(tfsIds) == 0:
        Log.error("tfsIds 字段不可为空")
        return
    req_info = Handle.req_info(
        api_index='TaskflowstatusSearch',
        path_change={"projectId" : projectId},
        body_change={"tfsIds" : f'"{tfsIds}"'}
    )
    response = Handle.api_req(req_info)
    try:
        if response['code'] == 200:
            return response
        else:
            Log.error(f"搜索项目工作流状态 返回异常 ==> {response}\n")
            return
    except Exception as e:
        Log.error(e)
        return
    
# 获取企业部门列表
def OrgDepartments(pageToken="\"\""):
    orgId = Handle.Tenant_Id
    req_info = Handle.req_info(
        api_index='OrgDepartments',
        body_change={"orgId" : f"\"{orgId}\"", "pageToken" : pageToken}
    )
    response = Handle.api_req(req_info)
    try:
        if response['code'] == 200:
            return response
        else:
            Log.error(f"获取企业部门列表 返回异常 ==> {response}\n")
            return
    except Exception as e:
        Log.error(e)
        return
    
# 获取部门成员列表
def DepartmentMembers(deptId, pageToken="\"\""):
    orgId = Handle.Tenant_Id
    req_info = Handle.req_info(
        api_index='DepartmentMembers',
        body_change={"orgId" : f"\"{orgId}\"", "pageToken" : pageToken, "deptId" : f"\"{deptId}\""}
    )
    response = Handle.api_req(req_info)
    try:
        if response['code'] == 200:
            return response
        else:
            Log.error(f"获取部门成员列表 返回异常 ==> {response}\n")
            return
    except Exception as e:
        Log.error(e)
        return
    
# 获取任务详情
def TaskQuery(taskId):
    orgId = Handle.Tenant_Id
    req_info = Handle.req_info(
        api_index='TaskQuery',
        body_change={"taskId" : f"\"{taskId}\""}
    )
    response = Handle.api_req(req_info)
    try:
        if response['code'] == 200:
            return response
        else:
            Log.error(f"获取任务详情 返回异常 ==> {response}\n")
            return
    except Exception as e:
        Log.error(e)
        return